package com.neutech.view;

import com.neutech.dao.AdminDAO;
import com.neutech.dao.impl.AdminDAOImpl;
import com.neutech.entity.Admin;

import static com.neutech.constant.Constant.input;

public class AdminView {

    private static AdminDAO adminDAO = new AdminDAOImpl();

    public static void showMeau(){
        System.out.println("欢迎使用饿了么管理系统");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");
        System.out.println("请输入编号选择功能");
        int index = input.nextInt();
        if (index==1){
            shouLogin();
        } else if (index==2) {
            register();
        } else if (index==3) {

        }else {
            System.err.println("没有此功能");
        }
    }

    public static void register(){
        //请输入用户名
        System.out.println("请输入用户名");
        String adminName = input.next();
        System.out.println("请输入密码");
        String password = input.next();
        //通过用户名查询数据库
        Admin admin = adminDAO.selectByAdminName(adminName);

        if (admin.getAdminId()!=null){
            System.err.println("用户已存在");
        }else {
            Admin adminDate = new Admin();
            adminDate.setAdminName(adminName);
            adminDate.setPassword(password);
            adminDAO.insert(adminDate);
            System.err.println("注册成功");

        }
        showMeau();

    }
    public static void shouLogin(){
        System.out.println("请输入用户名");
        String adminName = input.next();
        System.out.println("请输入密码");
        String password = input.next();

        Admin admin = adminDAO.selectByAdminName(adminName);

//        //TODO 登录逻辑
//        boolean isLogin = true;

        if(admin.getAdminName()==null){
            System.err.println("没有此用户");
            showMeau();
        }else if (!admin.getPassword().equals(password)){
            System.out.println("密码不正确");
        }else {
            System.out.println("登录成功");
            BusinessView.showMeau();
        }
    }
}
